import FormBoListApi from "@/common/api/form/formBoList.js";
import FormSolutionApi from "@/common/api/form/formSolution.js";
import AppUtil from "./AppUtil";

/**
 * 打开公共的对话框
 * @type {{}}
 */
let DialogBox = {};
import {Util} from '@lowcode/jpaas-base-lib'
import i18LangUtil from "@/language/lang/i18LangUtil";
import InsNewsDetail from "@/views/modules/portal/InsNews/InsNewsDetail.vue"
import DynamicView from "@/views/modules/common/DynamicView.vue"
import FormBoListPreview from "../../views/modules/form/FormBoListPreview/FormBoListPreview.vue";
import FormBusSolutionShow from "../../views/modules/form/FormBusSolutionShow/FormBusSolutionShow.vue";
import BpmDefDialog from "../../views/modules/bpm/components/dialog/BpmDefDialog.vue";
import OpenTabsByData from "../../views/modules/form/FormSolutionShow/OpenTabsByData.vue"
/**
 * 国际化
 */
function i18n(name, text) {
    let lang=localStorage.getItem("language") || "zh-CN";
    let langObj=i18LangUtil.langeFilesHandler(lang);
    return langObj["dialogBoxLang"][name] || text || "";
}

/**
 * 根据vue组件路径加载vue组件.
 * @param {String} view vue 组件路径.
 * @returns {*}
 */
DialogBox.loadView = function (view){
    return AppUtil.loadComponent(view)
}

/**
 * 打开表单方案
 * @example
 *  DialogBox.showForm({
        title:title,
        curVm: self,
        data: {alias: alias, pkId: pkId, readOnly: readOnly, parent: parent},
        max:true
      }, function (action) {
        self.onRefresh();
      });
 * @param config
 * @param callback
 */
DialogBox.showForm = async function (config, callback) {
    if(!config.data.alias) {
        return;
    }
    let appId = config.appId;
    if(appId){
        config.data['appId'] = appId
    }
    let record = await FormSolutionApi.getFormSolutionByAlias(config.data.alias,appId);
    //是否是树形表单方案
    let view =record.tree == 1 ? "modules/form/FormSolutionShow/FormSolutionTreeShow.vue": "modules/form/FormSolutionShow/FormSolutionShow.vue"
    let  component = DialogBox.loadView(view);
    let  title = config.title ? config.title : i18n('useForm', '使用表单');
    let  baseConf = {
        component: component,
        title: title
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if(callback){
            callback(action,data);
        }
    });
}
/**
 * 使用tab打开表单方案
 * @param allConfig 按钮配置和初始化数据
 * @param callback
 * @returns {Promise<void>}
 */
DialogBox.openFormByDataInTab = async function (allConfig, callback){
    let  baseConf = {
        component: OpenTabsByData,
        data:{
            allInitConfig:allConfig.objList
        }
    };
    let conf = {...baseConf, ...allConfig};
    Util.open(conf, function (action,data) {
        if(callback){
            callback(action,data);
        }
    })
}

/**
 * 查看新闻明细
 * @example
 *  DialogBox.getNewsDetail({
       curVm: this,
       data: {pkId: pkId, single: true, readonly: true},
       widthHeight: ['1000px', '700px']
    }, function (self_, data) {
    });
 * @param {Object} config   配置数据
 * @param {Function} callback 回调函数
 */
DialogBox.getNewsDetail=function (config, callback){
    let baseConf= {
        component:InsNewsDetail,
        title: i18n('viewNews','查看新闻公告')
    };
    let conf = { ...baseConf,...config };
    Util.open(conf,function(action){
        if(callback){
            callback(action);
        }
    });
}

/**
 * 打开自定义组件。
 * @param config {
 *   component:"组件的URL",
 *   title:"标题",
 *   max:true,
 *   curVm:this,
 *   widthHeight:['800px','600px'],
 *   data:{user:{name:"ray",address:"gz"}},
 * }
 * @param callBack 回调方法 function(action,data){
 *
 * }
 */
DialogBox.openComponent = function (config, callBack) {
    let component = DialogBox.loadView(config.component);
    let title = config.title || i18n('custom','自定义对话框');

    let conf = {
        component: DynamicView,
        curVm: config.curVm,
        title: title,
        data: {currentView: component, data: config.data}
    };
    if (config.widthHeight) {
        conf.widthHeight = config.widthHeight;
    } else {
        conf.max = true;
    }
    if(config.hasOwnProperty("shade")){
        conf.shade = config.shade;
    }
    if (config.cancel) {
        conf.cancel = config.cancel;
    }
    Util.open(conf, function (action, data) {
        if (callBack) {
            let func=callBack.bind(this);
            func(action, data);
        }
    })
}

/**
 * 打开列表对话框。
 * @param parameter {key:"对话框KEY",params:"Q_NAME__S_EQ=老王"}
 * @param config  {max:true,widthHeight:[data.width + 'px', data.height + 'px']}
 * @param callback
 */
DialogBox.dialog = function (parameter, config, callback) {
    FormBoListApi.setConf(parameter).then(res => {
        if (!res.success) return;
        var data = res.data;
        if(!config.hasOwnProperty("max")){
            if(data.isMax=="NO"){
                config.widthHeight = [data.width + 'px', data.height + 'px'];
                config.max=false;
            }
            else{
                config.max=true;
            }
        }

        let obj = {alias: parameter.key};
        let _parameter = {...obj, ...parameter}
        var baseConf = {
            component: FormBoListPreview,
            data: _parameter,
            title: data.name
        };
        let conf = {...baseConf, ...config};
        Util.open(conf, function (action, data) {
            if (action != 'ok') return;
            if (callback) {
                callback(action,data);
            }
        })
    });
}

/**
 *
 * @param config
 * @param callback
 */
DialogBox.showBusSolutionForm = function (config, callback) {
    let title = "使用业务方案";
    let baseConf = {
        component: FormBusSolutionShow,
        title: title
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if(callback){
            callback(action,data);
        }
    });
}

/**
 * 流程定义选择对话框
 * @constructor
 */
DialogBox.openBpmDefDialog = function (config, callback) {
    let obj = {
        component: BpmDefDialog,
        title: '选择流程定义',
        widthHeight: ['1200px', '650px'],
    }
    let configs = {...obj, ...config};
    Util.open(configs, function (action, data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}

export default DialogBox;
